const { ccclass, property } = cc._decorator;
declare let i18n,DBManager,dbh;
import Utils = require("../../utils/Utils");
import BaseView from "../../component/BaseView";
import UIHelper = require("../../component/UIHelper");
import PopUIHelper = require("../../utils/PopUIHelper");
//成为C位 关卡1
@ccclass
export default class DeputyBecomeCView extends BaseView {
    @property(cc.Label)
    lblStep: cc.Label = null;
    @property(cc.Label)
    lblDes: cc.Label = null;
    @property(cc.Node)
    sunNode: cc.Node = null;
    @property(cc.Node)
    cloudNode1: cc.Node = null;
    @property(cc.Node)
    cloudNode2: cc.Node = null;
    @property(cc.Node)
    cloudNode3: cc.Node = null;
    @property(cc.Node)
    cloudNode4: cc.Node = null;

    @property(sp.Skeleton)
    spine:sp.Skeleton = null;

    //恭喜获得
    @property(cc.Node)
    endNode: cc.Node = null;
    //-310 270 -260 270
    _done1 = false;
    _done2 = false;
    _done3 = false;
    _done4 = false;
    _done = false;
    _isStart = false;
    onLoad() {
        Utils.audioManager.playBGM("huazhuang/deputyGame/deputyGameBg");
        this.cloudNode1.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove1, this);
        this.cloudNode1.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd1, this);
        this.cloudNode1.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd1, this);
        this.cloudNode2.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove2, this);
        this.cloudNode2.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd2, this);
        this.cloudNode2.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd2, this);
        this.cloudNode3.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove3, this);
        this.cloudNode3.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd3, this);
        this.cloudNode3.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd3, this);
        this.cloudNode4.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove4, this);
        this.cloudNode4.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd4, this);
        this.cloudNode4.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd4, this);
        //@ts-ignore
        this._isStart = this.node.openParam;
        this.lblStep.string = i18n.t("DEPUTY_GAME_STEP_TITLE1");
        this.lblDes.string = i18n.t("DEPUTY_GAME_STEP_DES1");
    };
    onClose(){
        Utils.audioManager.playDefaultBGM();
    };
    checkDone(){
        if(this._done1 && this._done2 && this._done3 && this._done4){
            this._done = true;
            this.cloudNode1.active  = false;
            this.cloudNode2.active  = false;
            this.cloudNode3.active  = false;
            this.cloudNode4.active  = false;
            this.spine.setAnimation(0,"2",true);
            //@ts-ignore
            this.scheduleOnce(()=>{
                this.endNode.active = true;
                Utils.audioManager.playSound("huazhuang/deputyGame/pass");
            },1);
        }else{
            this._done = false;
        }
    };

    onTouchMove1(event){
        if(this._done)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.cloudNode1.parent.convertToNodeSpaceAR(worldPoint);
        this.cloudNode1.x = localPoint.x;
        this.cloudNode1.y = localPoint.y;
    };
    onTouchEnd1(event){
        // this._done1 = this.cloudNode1.x < -310 || this.cloudNode1.x > 255;
        let sun = this.sunNode.getBoundingBox();
        let clould = this.cloudNode1.getBoundingBox();
        this._done1 = !sun.intersects(clould);
        this.checkDone();
    };

    onTouchMove2(event){
        if(this._done)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.cloudNode2.parent.convertToNodeSpaceAR(worldPoint);
        this.cloudNode2.x = localPoint.x;
        this.cloudNode2.y = localPoint.y;
    };
    onTouchEnd2(event){
        // this._done2 = this.cloudNode2.x < -245 || this.cloudNode2.x > 180;
        let sun = this.sunNode.getBoundingBox();
        let clould = this.cloudNode2.getBoundingBox();
        this._done2 = !sun.intersects(clould);
        this.checkDone();
    };

    onTouchMove3(event){
        if(this._done)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.cloudNode3.parent.convertToNodeSpaceAR(worldPoint);
        this.cloudNode3.x = localPoint.x;
        this.cloudNode3.y = localPoint.y;
    };
    onTouchEnd3(event){
        // this._done3 = this.cloudNode3.x < -280 || this.cloudNode3.x > 230;
        let sun = this.sunNode.getBoundingBox();
        let clould = this.cloudNode3.getBoundingBox();
        this._done3 = !sun.intersects(clould);
        this.checkDone();
    };

    onTouchMove4(event){
        if(this._done)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.cloudNode4.parent.convertToNodeSpaceAR(worldPoint);
        this.cloudNode4.x = localPoint.x;
        this.cloudNode4.y = localPoint.y;
    };
    onTouchEnd4(event){
        // this._done4 = this.cloudNode4.x < -320 || this.cloudNode4.x > 270;
        let sun = this.sunNode.getBoundingBox();
        let clould = this.cloudNode4.getBoundingBox();
        this._done4 = !sun.intersects(clould);
        this.checkDone();
    };

    onClickEnd(isSkip){
        let next = this.params.next && this.params.next.shift();
        if (next) {
            UIHelper.openUI(next, {
                next: this.params.next
            })
            this.node.destroy();
        }
    };

    onClickSkip(){
        this.onClickEnd(true);
    };

    //提示
    onClickTips(){
        PopUIHelper.helper.showNewConfirmMsg(i18n.t("DEPUTY_GAME_TIPS1"));
    };
    
}
